export function getAudioDurationByFile(file) {
  return new Promise(resolve => {
    const audio = new Audio();
    const reader = new FileReader();
    // 当文件读取完毕后，创建一个blob URL
    reader.onload = function(event) {
      audio.onloadedmetadata = function() {
        resolve(audio.duration);
      };

      audio.onerror = function(e) {
        resolve(0);
      };
      audio.src = event.target.result;
      audio.load();
    };

    reader.readAsDataURL(file);
  });
}

export function playAudio(fileUrl) {
  const audio = new Audio();
  audio.onloadedmetadata = function() {
    audio.play();
  };

  audio.onerror = function(e) {
    alert(e.message || "音频加载失败");
  };
  audio.src = fileUrl;
}
